package com.suzus.aiService.controller;


import com.suzus.aiService.common.result.Result;
import com.suzus.aiService.pojo.dto.HospitalDTO;
import com.suzus.aiService.pojo.dto.NameDTO;
import com.suzus.aiService.pojo.entity.Medicine;
import com.suzus.aiService.pojo.vo.HospitalVO;
import com.suzus.aiService.service.AskService;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Set;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/ask")
public class AskController {
    @Autowired
    AskService askService;
    @Autowired
    private RedisTemplate redisTemplate;

    //此接口为输入药品大概名称返回药品信息
    @PostMapping("/searchMedicine")
    public Result<Medicine> searchMedicine(@RequestBody NameDTO name) throws JsonProcessingException, UnsupportedEncodingException {
        log.info(name.getName());
        Medicine medicine = askService.getMedicineByName(name.getName());
        return Result.success(medicine);
    }

    @PostMapping("/getHospitalByCityAndDept")
    public Result<List<HospitalVO>> getHospitalByCityAndDept(@RequestBody HospitalDTO hosp){
        System.out.println(hosp);
        return Result.success(askService.getHospitalByCityAndDept(hosp));
    }

    //此接口为测试大模型问答接口，不会用于前端
    @PostMapping("/ask")
    public Result ask(@RequestBody String question) throws Exception {
        return Result.success(askService.askMedicine(question));
    }
    //此接口用于清空redis的数据，仅测试用
    @PostMapping("/clear")
    public Result clearRedis(){
        Set<String> keys = redisTemplate.keys("*");
        int i=1;
        if (keys != null) {
            redisTemplate.delete(keys);
            log.info(i++ +"已被清理");
        }
        return Result.success();
    }


}
